//获取应用实例
const app = getApp();
const moment = require('../../utils/moment-with-locales.min.js');
moment.locale('zh-cn');
Component({
  options: {
    addGlobalClass: true,
  },
  properties: {
    newCount: {
      type: Number,//类型
      value: 0//默认值
    },
    hasUserInfo: {
      type: Boolean,//类型
      value: false//默认值
    }
  },
  data: {
    TabCur: 0,
    tabCur0Timer: null,
    tabCur0Loading: false,
    tabCur1Loading: false,
    tabCur0Data: [],
    tabCur1Data: [],
    countDown: ""
  },
  attached() {
    this.initTabList();
  },
  methods: {
    tabSelect(e) {
      var newId = e.currentTarget.dataset.id;
      this.setData({
        TabCur: newId
      });
      this.initTabList();
    },
    initTabList() {
      const index = this.data.TabCur;
      if (app.globalData.userInfo) {
        var that = this;
        const param = {
          sessionKey: app.globalData.sessionId
        };
        if (index == 0) {
          this.setData({
            tabCur0Loading: true
          });
          param.type = "4";
        } else if (index == 1) {
          this.setData({
            tabCur1Loading: true
          });
          param.type = "5";
        }
        wx.request({
          url: app.url.getListData,
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: param,
          success: function (res) {
            const data = res.data;
            const list = data.data;
            if (data.result == "true") {
              if (index == 0) {
                that.formatCountDown(list);
                if (that.data.tabCur0Timer) {
                  clearInterval(that.data.tabCur0Timer);
                }
                const tabCur0Timer = setInterval(function () {
                  that.formatCountDown(list);
                }, 1000);
                that.setData({
                  tabCur0Timer: tabCur0Timer
                });
              } else if (index == 1) {
                that.formatCountDown(list);
                that.setData({
                  tabCur1Loading: false,
                  tabCur1Data: list
                });
              }
            } else {
              wx.showModal({
                content: data.message,
                showCancel: false,
                success: function (res) { }
              });
              that.setData({
                tabCur0Loading: false,
                tabCur1Loading: false,
                tabCur2Loading: false
              });
            }
          },
          fail: res => {
            wx.showModal({
              content: "获取列表数据失败！",
              showCancel: false,
              success: function (res) { }
            });
          }
        });
      }
    },
    doDetail(e) {
      if (!this.checkMobile()) {
        this.cantOperation();
        return;
      }
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/detail/detail?id=' + id
      })
    },
    toLogin(e) {
      wx.navigateTo({
        url: '/pages/login/login'
      });
    },
    doAccept(e) {
      if (!this.checkMobile()) {
        this.cantOperation();
        return;
      }
      var id = e.currentTarget.dataset.id;
      var that = this;
      wx.showModal({
        title: '提醒',
        content: '确定接单吗？',
        confirmText: '立即接单',
        success(res) {
          if (res.confirm) {
            that.doOperation(id, "1");
          } else if (res.cancel) { }
        }
      });
    },
    doFinish(e) {
      if (!this.checkMobile()) {
        this.cantOperation();
        return;
      }
      var id = e.currentTarget.dataset.id;
      var that = this;
      wx.showModal({
        title: '提醒',
        content: '确定完成该接单吗？',
        confirmText: '完成',
        confirmColor: '#39b54a',
        success(res) {
          if (res.confirm) {
            that.doOperation(id, "3");
          } else if (res.cancel) { }
        }
      });
    },
    doOperation(id, type) {
      wx.showLoading({
        title: '请稍后...',
        mask: true
      });
      const param = {
        id: id,
        type: type,
        sessionKey: app.globalData.sessionId
      };
      var that = this;
      wx.request({
        url: app.url.doOpration,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: param,
        success: function (res) {
          wx.hideLoading();
          const data = res.data;
          if (data.result == 'true') {
            wx.showToast({
              title: data.message,
              icon: 'success',
              duration: 2000
            });
          } else {
            wx.showToast({
              title: data.message,
              image: '/images/error1.png',
              duration: 2000
            });
          }
          that.initTabList();
        },
        fail: res => {
          wx.showModal({
            content: "操作失败，请重试！",
            showCancel: false,
            success: function (res) { }
          });
        }
      });
    },
    checkMobile() {
      return !!app.globalData.userInfo.mobilePhone;
    },
    cantOperation() {
      wx.showModal({
        title: '警告',
        showCancel: false,
        confirmText: "去填写",
        content: '您还未填写快递员信息，不能接单！',
        success(res) {
          wx.navigateTo({
            url: '/pages/register/register'
          });
        }
      });
    },
    getCountDown(dateStr, delayMinute) {
      const diff = moment(dateStr).add(delayMinute, 'minute').diff(moment(), 'seconds');
      if (diff <= 0) {
        return null;
      }
      var diffMinute = parseInt(diff / 60);
      var diffSecond = diff % 60
      var countDownStr = (diffMinute > 0 ? (diffMinute + '分') : '') + diffSecond + '秒';
      return countDownStr;
    },
    formatCountDown(list) {
      for (const obj of list) {
        obj.fromNow = moment(obj.orderTime).fromNow();
        obj.countDown = this.getCountDown(obj.orderTime, obj.delayMinute);
        if (!obj.countDown) {
          obj.overTime = true;
        }
      }
      this.setData({
        tabCur0Loading: false,
        tabCur0Data: list
      });
    }
  }
})